// var username = '';
function getName() {
  return location.search.slice(1).split('=')[1];
}
var username = getName();
console.log(getName());
// 判断是否存在 name 用户名
if (!username) {
  location.href = "../login/index.html";
}
var tName = document.getElementById("t_name");
var navBtn = document.getElementById("left_btn");
var rightCont = document.getElementById("right_cont");
tName.innerHTML = '你好,' + username;
// 事件委托， 给ul 添加点击事件
navBtn.onclick = function (e) {
  var chidren = this.children;
  for (var i = 0; i < chidren.length; i++) {
    // 移除类名active
    chidren[i].classList.remove('active')
  }
  // 添加类名active
  e.target.classList.add('active');

  // 获取自定义属性
  var goal = e.target.getAttribute("goal");
  // 获取对应的标签内容 
  var content = rightCont.getElementsByClassName(goal)[0];
  if (content) {
    // 移除类名
    for (var i = 0; i < rightCont.children.length; i++) {
      rightCont.children[i].classList.remove("active_cont")
    }
    // 添加类名
    content.classList.add("active_cont");
  }
}

// 点击添加事件
var addForm = document.getElementById("add_form");
var addBtn = document.getElementById("add_btn");
var resetBtn = document.getElementById("reset_btn");
var appkey = 'hesong_1618800008142';
addBtn.onclick = function () {

  var elemens = addForm.elements;
  var data = {};
  data.appkey = appkey;
  data.name = elemens.name.value;
  data.sNo = elemens.sNo.value;
  data.sex = elemens.sex.value;
  data.birth = elemens.birth.value;
  data.phone = elemens.phone.value;
  data.address = elemens.address.value;
  data.email = elemens.email.value;
  ajax({
    url: "https://api.duyiedu.com/api/student/addStudent",
    type: "GET",
    params: data,
    success(value) {
      var obj = JSON.parse(value);
      if (obj.status === "fail") {
        alert("错误信息:" + obj.msg);
      } else {
        alert(obj.msg);
        addForm.reset();
      }
    }
  })
}

// 查询所有学生列表
var listBody = document.getElementById("list_body");
var studentList = [];
ajax({
  url: "https://api.duyiedu.com/api/student/findAll",
  type: "GET",
  params: {
    appkey
  },
  success(value) {
    var obj = JSON.parse(value)
    studentList = obj.data;
    stuList(studentList)

  }
})
// 学生列表
function stuList(list) {
  listBody.innerHTML = ''
  for (var i = 0; i < list.length; i++) {
    var stu = list[i];
    var trTag = document.createElement("tr");
    listBody.appendChild(trTag);
    trTag.innerHTML = `<tr>
    <td>${stu.sNo}</td>
    <td>${stu.name}</td>
    <td>${stuSex(stu.sex)}</td>
    <td>${stuBirth(stu.birth)}</td>
    <td>${stu.phone}</td>
    <td>${stu.email}</td>
    <td>${stu.address}</td>
    <td>
      <span class='edit_bar' index='${i}'>编辑</span>
      <span class='Remove' index='${i}'>删除</span>
    </td>
  </tr>`
  }
}

function stuSex(val) {
  return val === 1 ? '女' : '男';
}

function stuBirth(val) {
  return new Date().getFullYear() - val;
}

// 编辑事件
var editForm = document.getElementById("edit_form");
var Dialog = document.getElementById("Dialog");
listBody.onclick = function (e) {
  var btn = e.target;
  if (btn.classList.contains('edit_bar')) {
    Dialog.style.display = 'block';
    // 获取点击的每个编辑的索引值
    var index = btn.getAttribute("index");
    // 获取表单对应的所有input元素
    var elemens = editForm.elements;
    // console.log(elemens);
    // 获取点击的每个对应的详细列表
    var activeList = studentList[index];
    // console.log(activeList);
    for (var prop in activeList) {
      // 如果存在 则赋值
      if (elemens[prop]) {
        elemens[prop].value = activeList[prop];
      }
    }
  } else if (btn.classList.contains('Remove')) {
    var flag = window.confirm("确认删除吗?");
    if (flag) {
      var index = btn.getAttribute("index");
      var sNo = studentList[index].sNo;
      console.log(index);
      ajax({
        url: "https://api.duyiedu.com/api/student/delBySno",
        type: 'GET',
        params: {
          appkey: appkey,
          sNo: sNo
        },
        success(value) {
          console.log(value);
          var res = JSON.parse(value)
          if (res.status === "success") {
            alert(res.msg);
            location.reload();
          } else {
            alert("错误信息" + res.msg)
          }
        }
      })
    }

  }


}

Dialog.onclick = function () {
  this.style.display = 'none';
}
// 取消事件冒泡
editForm.onclick = function (e) {
  e.stopPropagation();
}

// 修改事件
var editBtn = document.getElementById("edit_btn");
var appkey = 'hesong_1618800008142';
editBtn.onclick = function () {
  console.log(appkey);
  var elemens = editForm.elements;
  var data = {};
  data.appkey = appkey;
  data.name = elemens.name.value;
  data.sNo = elemens.sNo.value;
  data.sex = elemens.sex.value;
  data.birth = elemens.birth.value;
  data.phone = elemens.phone.value;
  data.address = elemens.address.value;
  data.email = elemens.email.value;
  ajax({
    url: "https://api.duyiedu.com/api/student/updateStudent",
    type: "GET",
    params: data,
    success(value) {
      var obj = JSON.parse(value);
      if (obj.status === 'fail') {
        alert("错误信息" + obj.msg)
      } else {
        alert(obj.msg);
        Dialog.style.display = 'none';
        // 页面刷新
        location.reload();
      }
    }
  })
}

// 分页事件
var stList = []; // 装列表
var nowPage = 1; // 当前页
var size = 2; // 每页展示的个数
var totalPage = 1;
var Page = document.getElementById("page");
// 获取当前页数对应列表函数
function getDataPage() {
  // Page.innerHTML='';
  ajax({
    url: "https://api.duyiedu.com/api/student/findByPage",
    type: "GET",
    params: {
      appkey,
      page: nowPage,
      size: size
    },
    success(res) {
      var result = JSON.parse(res);
      totalPage = Math.ceil(result.data.cont / size);
      stList = result.data.findByPage;
      // console.log(stList);
      stuList(stList);
      renderPage();
    }
  })
}
// 页数
function renderPage() {
  Page.innerHTML = '';
  for (var i = 1; i <= totalPage; i++) {
    var liTag = document.createElement('li');
    liTag.innerHTML = i;
    Page.appendChild(liTag);
    if (i === nowPage) {
      liTag.classList.add("active");
    }
  }
}
getDataPage();

// 上一页下一页事件
Page.onclick = function (e) {
  var tar = e.target;
  console.log(tar.tagName);
  if (tar.tagName === 'LI') {
    // 点击，切换当前页 
    nowPage = Number(tar.innerHTML);
    // 根据当前页数 获取当前页数对应的数据
    getDataPage();
  }
}

var leftBtn = document.getElementById("lef_btn");
leftBtn.onclick = function () {
  if (nowPage > 1) {
    nowPage -= 1;
    getDataPage();
  }
}
var rightBtn = document.getElementById("right_btn");
rightBtn.onclick = function () {
  if (nowPage < totalPage) {
    nowPage += 1;
    getDataPage();
  }
}